package ${properties.controllerPkgName};

import java.util.HashMap;
<#if (importKey?size > 0)>
import java.util.List;
</#if>

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import ${properties.boPkgName}.${className}BO;
<#list importKey as impKey>
import ${properties.pojoPkgName}.${impKey.keyTableClassName};
import ${properties.boPkgName}.${impKey.keyTableClassName}BO;
</#list>
import ${properties.pojoPkgName}.${className};



public class ${className}Controller extends SuperMultiActionController implements InitializingBean{
	
	protected static final Logger logger = LoggerFactory.getLogger(${className}Controller.class);

	private ${className}BO ${className?uncap_first}BO;
<#list importKey as impKey>	
	private ${impKey.keyTableClassName}BO ${impKey.keyTableClassName?uncap_first}BO;
</#list>
   
	public ${className}BO get${className}BO() {
		return ${className?uncap_first}BO;
	}

	public void set${className}BO(${className}BO ${className?uncap_first}BO) {
		this.${className?uncap_first}BO = ${className?uncap_first}BO;
	}
<#list importKey as impKey>
	public void set${impKey.keyTableClassName}BO(${impKey.keyTableClassName}BO ${impKey.keyTableClassName?uncap_first}BO) {
		this.${impKey.keyTableClassName?uncap_first}BO = ${impKey.keyTableClassName?uncap_first}BO;
	}
</#list>	
	
	public void afterPropertiesSet() throws Exception
	{
	    if(${className?uncap_first}BO == null)
	      throw new ApplicationContextException("Must set ${className?uncap_first}BO property on " + getClass());
<#list importKey as impKey>
	 	if(${impKey.keyTableClassName?uncap_first}BO == null)
	      throw new ApplicationContextException("Must set ${impKey.keyTableClassName?uncap_first}BO property on " + getClass());
</#list>		      
	}
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap<String,Object> map = new HashMap<String,Object>();
		int pageSize = ServletRequestUtils.getIntParameter(request, "pageSize", 20);
		int currentPageNo = ServletRequestUtils.getIntParameter(request, "currentPageNo", 1);
		${properties.daoPkgName}.BaseDAO.Page page = get${className}BO().getBaseDAO().getPage("from ${properties.pojoPkgName}.${className}",pageSize,currentPageNo);
	    map.put("page", page);
	    return new ModelAndView("/${properties.projectName?uncap_first}/sample/${className?uncap_first}/list.jsp",map);
	}
	
	public ModelAndView show(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap<String,Object> map = new HashMap<String,Object>();
	    ${className} ${className?uncap_first} = this.${className?uncap_first}BO.get${className}(ServletRequestUtils.getLongParameter(request,"${pk?uncap_first}"));
	    map.put("${className?uncap_first}", ${className?uncap_first});
	    return new ModelAndView("/${properties.projectName?uncap_first}/sample/${className?uncap_first}/show.jsp",map);
	}
	
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap<String,Object> map = new HashMap<String,Object>();
<#list importKey as impKey>
		List<${impKey.keyTableClassName}> ${impKey.keyTableClassName?uncap_first}List = this.${impKey.keyTableClassName?uncap_first}BO.get${impKey.keyTableClassName}s();
		map.put("${impKey.keyTableClassName?uncap_first}List", ${impKey.keyTableClassName?uncap_first}List);
</#list>
	    return new ModelAndView("/${properties.projectName?uncap_first}/sample/${className?uncap_first}/add.jsp",map);
	}	
	
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap<String,Object> map = new HashMap<String,Object>();
	    ${className} ${className?uncap_first} = this.${className?uncap_first}BO.get${className}(ServletRequestUtils.getLongParameter(request,"${pk?uncap_first}"));
	    map.put("${className?uncap_first}",${className?uncap_first});
<#list importKey as impKey>
		List<${impKey.keyTableClassName}> ${impKey.keyTableClassName?uncap_first}List = this.${impKey.keyTableClassName?uncap_first}BO.get${impKey.keyTableClassName}s();
		map.put("${impKey.keyTableClassName?uncap_first}List", ${impKey.keyTableClassName?uncap_first}List);
</#list>
	    return new ModelAndView("/${properties.projectName?uncap_first}/sample/${className?uncap_first}/edit.jsp",map);
	}
	
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap<String,Object> map = new HashMap<String,Object>();
		${className} ${className?uncap_first} = null;
	    if(request.getParameter("${pk?uncap_first}") != null)
	    	${className?uncap_first} = this.${className?uncap_first}BO.get${className}(ServletRequestUtils.getLongParameter(request,"${pk?uncap_first}"));
	    else
	    	${className?uncap_first} = new ${className}();
<#list fields as fld>
		<#if (fld.classType="Integer")>${className?uncap_first}.set${fld.className}(ServletRequestUtils.getIntParameter(request,"${fld.className?uncap_first}"));		
		<#else>${className?uncap_first}.set${fld.className}(ServletRequestUtils.get${fld.classType}Parameter(request,"${fld.className?uncap_first}"));
		</#if>
	   
</#list>
<#list importKey as impKey>
	    ${className?uncap_first}.set${impKey.keyTableClassName}(this.${impKey.keyTableClassName?uncap_first}BO.get${impKey.keyTableClassName}(ServletRequestUtils.getLongParameter(request,"${impKey.keyColumnClassName?uncap_first}")));
</#list>
	    this.${className?uncap_first}BO.save(${className?uncap_first});
	    map.put("${className?uncap_first}", ${className?uncap_first});
<#list importKey as impKey>
		List<${impKey.keyTableClassName}> ${impKey.keyTableClassName?uncap_first}List = this.${impKey.keyTableClassName?uncap_first}BO.get${impKey.keyTableClassName}s();
		map.put("${impKey.keyTableClassName?uncap_first}List", ${impKey.keyTableClassName?uncap_first}List);
</#list>
	    return new ModelAndView("/${properties.projectName?uncap_first}/sample/${className?uncap_first}/show.jsp",map);
	}	
		  
}